home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Technotools
/
Technotools (Chestnut CD-ROM)(1993).ISO
/
lang_c
/
msqc25t1
/
exec.c
< prev
next >
Wrap
C/C++ Source or Header
|
1990-09-04
|
1KB
|
53 lines
/* exec.c: Uses exec() to invoke a child process */
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#include <dos.h>
#include <errno.h>
#include <graph.h>
void main()
{
char newvar[] = "XYZ=7890", /* New environment srting */
*envp [5], /* pointers to env strings */
childpath[] = "CHILD.EXE", /* path to child */
*args[] = {"CHILD.EXE", /* command line arguments */
"A1", "A2", NULL},
comspec [64], path [64], prompt [64];
/* Show current environment */
_clearscreen (_GCLEARSCREEN);
puts ("In parent, orginal environment is:\n");
system ("SET");
/* Get current environment strings for child */
sprintf (comspec, "COMSPEC= %s", getenv ("COMSPEC"));
sprintf (path, "PATH=%s", getenv ("PATH"));
sprintf (prompt, "PROMPT=%s", getenv ("PROMPT"));
/* Load pointer array for environment strings */
envp [0] = comspec;
envp [1] = path;
envp [2] = prompt;
envp [3] = newvar;
envp [4] = NULL;
/* Chain to the child */
puts ("\nChaining to child process\n");
execvpe (childpath, args, envp);
/* This code runs only if there was an error */
puts ("\nError occurred:\n");
switch (errno) {
case E2BIG: puts ("Argument list to long"); break;
case EINVAL: puts ("Invalid argument"); break;
case ENOENT: puts ("Bad path or filename"); break;
case ENOEXEC: puts ("Exec format error"); break;
case ENOMEM: puts ("Not enough memory"); break;
}
}